﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ADC.Utilites;
using ADC.BL;
using ADC.DAL;

namespace ADC.Web.Admin
{
    public partial class ListProduct : System.Web.UI.Page
    {
        log4net.ILog logger = log4net.LogManager.GetLogger("ListProduct");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Page.Title = Common.PAGE_TITLE + "Danh sách sản phẩm";
                //load data for DropDownListProgrameType
                List<DAL.ProductType> productType = ProductTypeFactory.SelectAll();
                foreach (DAL.ProductType pt in productType)
                {
                    DropDownListProdcutType.Items.Add(new ListItem(pt.Title, pt.ProductTypeID + ""));
                }

                BindData();
            }
        }


        protected void DropDownListProdcutType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }


        private void BindData()
        {
            try
            {
                var productType = ProductFactory.GetAll(Convert.ToInt32(DropDownListProdcutType.SelectedValue));
                GridViewPrograme.AutoGenerateColumns = false;
                GridViewPrograme.DataSource = productType;
                GridViewPrograme.DataBind();
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewPrograme_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                var lblProductID = (Label)GridViewPrograme.Rows[e.NewEditIndex].FindControl("lblProductID");
                if (lblProductID != null && !string.IsNullOrEmpty(lblProductID.Text))
                {
                    Response.Redirect("ManageProduct.aspx?Mode=1&ID=" + lblProductID.Text + "&Type=" + DropDownListProdcutType.SelectedValue, false);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void GridViewPrograme_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                var lblProductID = (Label)GridViewPrograme.Rows[e.RowIndex].FindControl("lblProductID");
                if (lblProductID != null && !string.IsNullOrEmpty(lblProductID.Text))
                {
                    var productID = int.Parse(lblProductID.Text);

                    int result = ProductFactory.Delete(productID);
                    BindData();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsEnableDebugMode) throw;
            }
        }

        protected void LinkButtonAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageProduct.aspx?Mode=0&Type=" + DropDownListProdcutType.SelectedValue, false);
        }
    }
}